package com.qc31.monitorlib.ui.alarms

import android.widget.TextView
import androidx.recyclerview.widget.LinearLayoutManager
import autodispose2.autoDispose
import com.qc31.baselib.base.BaseBarActivity
import com.qc31.baselib.custom.MToolbar
import com.qc31.baselib.utils.ToolbarHelper
import com.qc31.baselib.utils.isFastClick
import com.qc31.baselib.utils.setResultTo
import com.qc31.baselib.utils.showToast
import com.qc31.comlib.Keys
import com.qc31.comlib.alarm.AlarmDataUtil
import com.qc31.comlib.entity.AlarmCount
import com.qc31.comlib.net.ServiceHelper
import com.qc31.monitorlib.R
import com.qc31.monitorlib.databinding.ActivitySubAlarmBinding
import com.qc31.monitorlib.ui.adapter.ChooseAlarmAdapter

/**
 *@Author Jasper
 *@Time   2020/11/30
 *@Desc   订阅报警
 */
class SubAlarmActivity :
    BaseBarActivity<ActivitySubAlarmBinding>(ActivitySubAlarmBinding::inflate) {
    private var ids: String = ""
    private val mAdapter by lazy { ChooseAlarmAdapter() }

    override fun initView() {
        ToolbarHelper.setToolbar(
            this,
            R.string.title_monitor_sub_alarm,
            R.string.desc_com_sure,
            object : MToolbar.IToolbarCallback {
                override fun actionOnClick(action: TextView) {
                    if (isFastClick()) return
                    updateAlarms()
                }
            }
        )

        binding.recyclerAlarm.layoutManager = LinearLayoutManager(this)
        binding.recyclerAlarm.adapter = mAdapter

        initAlarmData()
    }

    override fun setListener() {

        binding.tvAlarmAllCheck.setOnClickListener {
            binding.tvAlarmAllCheck.isSelected = true
            binding.tvAlarmAllUnCheck.isSelected = false
            checkAll()
        }
        binding.tvAlarmAllUnCheck.setOnClickListener {
            binding.tvAlarmAllCheck.isSelected = false
            binding.tvAlarmAllUnCheck.isSelected = true
            uncheckAll()
        }
        mAdapter.setAllBack(object : ChooseAlarmAdapter.ChooseAllBack {
            override fun onChooseAll() {
                setAllChoose()
            }
        })
    }

    /**
     * 1全选   0部分选择  -1全不选
     */
    private fun isAllBack(): Int {
        var allChoose = 0
        var allNotChoose = 0
        mAdapter.data.forEach {
            if (it.isAllChoose) {
                allNotChoose++
            } else {
                allChoose++
                it.children.forEach { entity ->
                    if (entity.isChoose) allNotChoose++
                }
            }
        }
        if (allChoose == 0) return 1
        if (allNotChoose == 0) return -1
        return 0
    }

    private fun initAlarmData() {
        val data = intent?.getParcelableArrayListExtra(Keys.INTENT_DATA)
            ?: ArrayList<AlarmCount>()

        for (count in data) {
            ids = "$ids,${count.alarmType}"
        }
        if (ids.isNotEmpty()) {
            ids = ids.substring(1)
        }
        handleData(ids)

    }

    // 全选
    private fun checkAll() {
        mAdapter.data.forEach {
            it.isAllChoose = true
            it.children.forEach { children ->
                children.isChoose = true
            }
        }
        mAdapter.notifyDataSetChanged()
    }

    //全不选
    private fun uncheckAll() {
        mAdapter.data.forEach {
            it.isAllChoose = false
            it.children.forEach { children ->
                children.isChoose = false
            }
        }
        mAdapter.notifyDataSetChanged()
    }


    private fun handleData(ids: String) {
        val alarmsByList = AlarmDataUtil.instance.getAlarmByIds(false, ids)
        mAdapter.setList(alarmsByList)
        setAllChoose()
    }

    private fun setAllChoose() {
        when (isAllBack()) {
            0 -> {
                binding.tvAlarmAllCheck.isSelected = false
                binding.tvAlarmAllUnCheck.isSelected = false
            }
            1 -> {
                binding.tvAlarmAllCheck.isSelected = true
                binding.tvAlarmAllUnCheck.isSelected = false
            }
            else -> {
                binding.tvAlarmAllCheck.isSelected = false
                binding.tvAlarmAllUnCheck.isSelected = true
            }
        }

    }

    private fun updateAlarms() {
        isShowLoading(true)
        val alarmIds = AlarmDataUtil.instance.getAlarmIds(mAdapter.data, false)
        ServiceHelper.getRepository().setAlarmType(alarmIds).autoDispose(scopeProvider)
            .subscribe({
                isShowLoading(false)
                setResultTo(this)
                finish()
            }, {
                showToast(getString(R.string.toast_update_faile) + it.message)
                isShowLoading(false)
            })
    }
}